import api from './index'

export interface Problem {
  id: number
  title: string
  description: string
  type: 'choice' | 'blank' | 'code' | 'short'
  difficulty: 'easy' | 'medium' | 'hard'
  answer?: string
  test_cases?: any[]
  options?: string[]
  points: number
  time_limit?: number
  memory_limit?: number
  language?: string
  input_format?: string
  output_format?: string
  created_by: number
  status: string
  created_at: string
}

export interface CreateProblemRequest {
  title: string
  description: string
  type: 'choice' | 'blank' | 'code' | 'short'
  difficulty: 'easy' | 'medium' | 'hard'
  answer?: string
  test_cases?: any[]
  options?: string[]
  points: number
  time_limit?: number
  memory_limit?: number
  language?: string
  input_format?: string
  output_format?: string
}

export interface UpdateProblemRequest {
  title?: string
  description?: string
  type?: 'choice' | 'blank' | 'code' | 'short'
  difficulty?: 'easy' | 'medium' | 'hard'
  answer?: string
  test_cases?: any[]
  options?: string[]
  points?: number
  time_limit?: number
  memory_limit?: number
  language?: string
  input_format?: string
  output_format?: string
  status?: string
}

// AI生成题目的请求参数
export interface GenerateCodeProblemRequest {
  topic: string
  difficulty?: 'easy' | 'medium' | 'hard'
  language?: 'python' | 'cpp' | 'java'
  knowledge_points?: string
  algorithm_type?: string
  auto_save?: boolean
}

export interface GenerateChoiceProblemRequest {
  topic: string
  difficulty?: 'easy' | 'medium' | 'hard'
  question_type?: 'single' | 'multiple'
  option_count?: number
  knowledge_point?: string
  auto_save?: boolean
}

export interface GenerateBlankProblemRequest {
  topic: string
  difficulty?: 'easy' | 'medium' | 'hard'
  blank_count?: number
  knowledge_point?: string
  auto_save?: boolean
}

export interface GenerateShortProblemRequest {
  topic: string
  difficulty?: 'easy' | 'medium' | 'hard'
  question_type?: 'concept' | 'analysis' | 'design'
  knowledge_point?: string
  auto_save?: boolean
}

// 获取题目列表
export function getProblems(params?: {
  type?: string
  difficulty?: string
  language?: string
  status?: string
  page?: number
  size?: number
}) {
  return api.get<Problem[]>('/problems/', { params })
}

// 获取单个题目
export function getProblem(id: number) {
  return api.get<Problem>(`/problems/${id}`)
}

// 创建题目
export function createProblem(data: CreateProblemRequest) {
  return api.post<Problem>('/problems/', data)
}

// 更新题目
export function updateProblem(id: number, data: UpdateProblemRequest) {
  return api.put<Problem>(`/problems/${id}`, data)
}

// 删除题目
export function deleteProblem(id: number) {
  return api.delete(`/problems/${id}`)
}

// AI生成编程题
export function generateCodeProblem(data: GenerateCodeProblemRequest) {
  return api.post('/llm/generate/code-problem', data)
}

// AI生成选择题
export function generateChoiceProblem(data: GenerateChoiceProblemRequest) {
  return api.post('/llm/generate/choice-problem', data)
}

// AI生成填空题
export function generateBlankProblem(data: GenerateBlankProblemRequest) {
  return api.post('/llm/generate/blank-problem', data)
}

// AI生成简答题
export function generateShortProblem(data: GenerateShortProblemRequest) {
  return api.post('/llm/generate/short-problem', data)
}

// 批量生成题目
export function batchGenerateProblems(data: {
  problem_type: 'code' | 'choice' | 'blank' | 'short'
  count: number
  topic: string
  difficulty?: 'easy' | 'medium' | 'hard'
  auto_save?: boolean
}) {
  return api.post('/llm/generate/batch', data)
}

// 生成测试用例
export function generateTestCases(problemDescription: string, language: string) {
  return api.post('/problems/generate-test-cases', {
    problem_description: problemDescription,
    language
  })
}

// 生成提示
export function generateHints(problemDescription: string, userProgress: string) {
  return api.post('/problems/generate-hints', {
    problem_description: problemDescription,
    user_progress: userProgress
  })
}

export const problemsApi = {
  // 获取所有题目
  getProblems(): Promise<{ problems: Problem[]; total: number; pages: number; current_page: number; per_page: number }> {
    return api.get('/problems?per_page=1000') // 获取所有题目，不分页
  },

  // 获取指定题目
  getProblem(id: number): Promise<Problem> {
    return api.get(`/problems/${id}`)
  },

  // 创建题目
  createProblem(data: CreateProblemRequest): Promise<Problem> {
    return api.post('/problems', data)
  },

  // 更新题目
  updateProblem(id: number, data: Partial<CreateProblemRequest>): Promise<Problem> {
    return api.put(`/problems/${id}`, data)
  },

  // 删除题目
  deleteProblem(id: number): Promise<void> {
    return api.delete(`/problems/${id}`)
  },

  // 按类型获取题目
  getProblemsByType(type: string): Promise<Problem[]> {
    return api.get(`/problems/type/${type}`)
  },

  // 搜索题目
  searchProblems(params: { keyword?: string; type?: string }): Promise<Problem[]> {
    return api.get('/problems/search', { params })
  },

  // AI生成编程题
  generateCodeProblem(data: GenerateCodeProblemRequest) {
    return api.post('/llm/generate/code-problem', data)
  },

  // AI生成选择题
  generateChoiceProblem(data: GenerateChoiceProblemRequest) {
    return api.post('/llm/generate/choice-problem', data)
  },

  // AI生成填空题
  generateBlankProblem(data: GenerateBlankProblemRequest) {
    return api.post('/llm/generate/blank-problem', data)
  },

  // AI生成简答题
  generateShortProblem(data: GenerateShortProblemRequest) {
    return api.post('/llm/generate/short-problem', data)
  },

  // 批量生成题目
  batchGenerateProblems(data: {
    problem_type: 'code' | 'choice' | 'blank' | 'short'
    count: number
    topic: string
    difficulty?: 'easy' | 'medium' | 'hard'
    auto_save?: boolean
  }) {
    return api.post('/llm/generate/batch', data)
  }
}
